package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.constant.Constant;
import com.ygqh.baby.mapper.YgShoppingCarMapper;
import com.ygqh.baby.model.*;
import com.ygqh.baby.po.YgShoppingCar;
import com.ygqh.baby.po.YgShoppingCarExample;
import com.ygqh.baby.po.YgSku;
import com.ygqh.baby.service.*;
import com.ygqh.baby.utils.CarComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@Service
public class YgShoppingCarServiceImpl implements YgShoppingCarService {

    @Autowired
    private YgShoppingCarMapper ygShoppingCarMapper;
    @Autowired
    private YgSkuService skuService;
    @Autowired
    private YgStockService stockService;
    @Autowired
    private YgUserService ygUserService;
    @Autowired
    private YgBmdismService ygBmdismService;
    @Autowired
    private DocumentConstant documentConstant;

    @Override
    public List<YgShoppingCar> find(Long userId) {
        YgShoppingCarExample example = new YgShoppingCarExample();
        example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name()).andUserIdEqualTo(userId);
        return ygShoppingCarMapper.selectByExample(example);
    }

    @Override
    public int findCount(Long userId) {
        YgShoppingCarExample example = new YgShoppingCarExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andUserIdEqualTo(userId);
        return ygShoppingCarMapper.countByExample(example);
    }

    @Override
    public YgShoppingCar findById(Long id) {
        return ygShoppingCarMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(YgShoppingCar ygShoppingCar) {
        return ygShoppingCarMapper.updateByPrimaryKeySelective(ygShoppingCar);
    }

    @Override
    public ResultSet<YgShoppingCar> search(QueryInfo queryInfo, String q) {
        int total = ygShoppingCarMapper.countSuper(q);
        if (total == 0) {
            return new ResultSet<>();
        }
        List<YgShoppingCar> result = ygShoppingCarMapper.selectSuper(queryInfo, q);
        return new ResultSet<>(total, result);
    }

    @Override
    public int deleteSoft(Long[] ids, Long userId) {
        Assert.notEmpty(ids);

        YgShoppingCarExample example = new YgShoppingCarExample();
        example.createCriteria().andIdIn(Arrays.asList(ids)).andUserIdEqualTo(userId);
        YgShoppingCar record = new YgShoppingCar();
        record.setStatus(DataStatus.Delete);

        return ygShoppingCarMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int deleteSoft(Long id, Long userId) {
        Assert.notNull(id);
        YgShoppingCarExample example = new YgShoppingCarExample();
        example.createCriteria().andIdEqualTo(id).andUserIdEqualTo(userId);
        YgShoppingCar record = new YgShoppingCar();
        record.setStatus(DataStatus.Delete);
        return ygShoppingCarMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int removeShoppingCar(Long userId) {
        return removeShoppingCar(userId, SalesPlatform.Yiigoo);
    }

    @Override
    public int removeShoppingCar(Long userId, SalesPlatform salesPlatform) {
        YgShoppingCarExample example = new YgShoppingCarExample();
        YgShoppingCarExample.Criteria criteria = new YgShoppingCarExample().createCriteria();
        criteria.andStatusEqualTo(DataStatus.Valid.name()).andUserIdEqualTo(userId);
        if (salesPlatform != null) {
            criteria.andSalesPlatformEqualTo(salesPlatform.name());
        }
        YgShoppingCar record = new YgShoppingCar();
        record.setStatus(DataStatus.Delete);
        return ygShoppingCarMapper.updateByExampleSelective(record, example);
    }

    @Override
    public List<YgShoppingCarModel> findShoppingCarList(QueryInfo queryInfo, Long userId) {
        return this.findShoppingCarList(queryInfo, userId, false);
    }

    @Override
    public List<YgShoppingCarModel> findShoppingCarList(QueryInfo queryInfo, Long userId, Boolean distribution) {
        return findShoppingCarList(queryInfo, userId, distribution, SalesPlatform.Yiigoo);
    }

    @Override
    public List<YgShoppingCarModel> findShoppingCarList(QueryInfo queryInfo, Long userId, Boolean distribution,
                                                        SalesPlatform salesPlatform) {
        List<YgShoppingCarModel> carList = ygShoppingCarMapper.selectShoppingCarList(queryInfo, userId, null,
                salesPlatform == null ? SalesPlatform.Yiigoo.name() : salesPlatform.name());
        boolean vipNew = isVipNew(userId, salesPlatform);
        carList.forEach(carModel -> resetCarSalePrice(carModel, vipNew, distribution));
        return carList;
    }

    private void resetCarSalePrice(YgShoppingCarModel car, Boolean vip, Boolean distribution) {
        if (PromotionType.GroupBuying.equals(car.getSalesType())) {
            car.setRealSalesPrice(car.getPromotionPrice());
        } else if (PromotionType.JiaJiaGou.equals(car.getSalesType())
                || PromotionType.ManZeng.equals(car.getSalesType())) {
            car.setVipPrice(BigDecimal.ZERO);
            car.setRealSalesPrice(car.getPromotionPrice());
        } else {
            car.setRealSalesPrice(car.getSalesPrice());
        }

        if (vip && car.getVipPrice().compareTo(BigDecimal.ZERO) > 0) {
            car.setRealSalesPrice(car.getVipPrice());
            car.setSalesPrice(car.getVipPrice());
        } else if (PromotionType.JiaJiaGou.equals(car.getSalesType()) || PromotionType.ManZeng.equals(car.getSalesType())) {
            car.setRealSalesPrice(car.getPromotionPrice());
        }
        if (distribution && car.getDistributionPrice() != null && car.getDistributionPrice().compareTo(BigDecimal.ZERO) > 0) {
            car.setSalesPrice(car.getDistributionPrice());
            car.setRealSalesPrice(car.getDistributionPrice());
        }

        if (vip && car.getPackVipPrice() != null && car.getPackVipPrice().compareTo(BigDecimal.ZERO) > 0) {
            car.setPackPrice(car.getPackVipPrice());
        } else if (distribution && car.getPackDistributionPrice() != null && car.getPackDistributionPrice().compareTo(BigDecimal.ZERO) > 0) {
            car.setPackPrice(car.getPackDistributionPrice());
        }
    }

    @Override
    public Message addPromotionItem(Long skuId, Long quantity, Long userId, PromotionType pType, BigDecimal promotionPrice) {
        return addPromotionItem(skuId, quantity, userId, pType, promotionPrice, SalesPlatform.Yiigoo);
    }

    @Override
    public Message addPromotionItem(Long skuId, Long quantity, Long userId, PromotionType pType,
                                    BigDecimal promotionPrice, SalesPlatform salesPlatform) {
        Boolean boo = this.validateStockNum(skuId, quantity);
        if (!boo) {
            return Message.error("库存不足", skuId);
        }
        if (pType.equals(PromotionType.ManZeng)) {
            promotionPrice = new BigDecimal(0);
        }
        YgShoppingCar shoppingCar = new YgShoppingCar();
        shoppingCar.setUserId(userId);
        shoppingCar.setQuantity(quantity);
        shoppingCar.setSkuId(skuId);
        shoppingCar.setSalesType(pType);
        shoppingCar.setPromotionPrice(promotionPrice);
        shoppingCar.setCreateTime(new Date());
        shoppingCar.setSalesPlatform(salesPlatform);
        if (pType.equals(PromotionType.ManZeng)) {
            shoppingCar.setIsCheck(true);
        }

        YgShoppingCar car = this.findBySkuId(null, pType, userId, salesPlatform);
        if (car != null) {
            // 赠品需新增一条记录且只存在一条
            car.setStatus(DataStatus.Delete);
            ygShoppingCarMapper.updateByPrimaryKeySelective(car);

        }
        ygShoppingCarMapper.insertSelective(shoppingCar);

        return Message.success(documentConstant.getMESSAGE_SUCCESS(), shoppingCar.getId());
    }

    @Override
    public int addOrdinaryItem(Long skuId, Long quantity, Long userId) {
        return addOrdinaryItem(skuId, quantity, userId, SalesPlatform.Yiigoo);
    }

    @Override
    public int addOrdinaryItem(Long skuId, Long quantity, Long userId, SalesPlatform salesPlatform) {
        int row = 0;
        Boolean boo = this.validateStockNum(skuId, quantity);
        if (!boo) {
            return row;
        }

        YgShoppingCar car = this.findBySkuId(skuId, PromotionType.Ordinary, userId, salesPlatform);
        if (car != null) {
            car.setQuantity(car.getQuantity() + quantity);
            car.setCreateTime(new Date());
            row = ygShoppingCarMapper.updateByPrimaryKeySelective(car);
            return row;
        }
        YgShoppingCar shoppingCar = new YgShoppingCar();
        shoppingCar.setUserId(userId);
        shoppingCar.setSkuId(skuId);
        shoppingCar.setQuantity(quantity);
        shoppingCar.setSalesType(PromotionType.Ordinary);
        shoppingCar.setCreateTime(new Date());
        shoppingCar.setSalesPlatform(salesPlatform);
        row = ygShoppingCarMapper.insertSelective(shoppingCar);
        return row;
    }

    @Override
    public int updateNum(Long skuId, Boolean isAdd, Long userId) {
        return updateNum(skuId, isAdd, userId, SalesPlatform.Yiigoo);
    }

    @Override
    public int updateNum(Long skuId, Boolean isAdd, Long userId, SalesPlatform salesPlatform) {
        int row;
        if (isAdd) {
            row = ygShoppingCarMapper.addItemNum(skuId, userId, YgShoppingCar.ONE, salesPlatform);
        } else {
            row = ygShoppingCarMapper.reduceItemNum(skuId, userId, YgShoppingCar.ONE, salesPlatform);

        }
        return row;
    }

    /**
     * 校验库存
     *
     * @param skuId
     * @param quantity
     * @return
     */
    private Boolean validateStockNum(Long skuId, Long quantity) {
        return stockService.validateStockNum(skuId, quantity);

    }

    @Override
    public YgShoppingCar findBySkuId(Long skuId, PromotionType pType, Long userId) {
        return findBySkuId(skuId, pType, userId, SalesPlatform.Yiigoo);
    }

    private YgShoppingCar findBySkuId(Long skuId, PromotionType pType, Long userId, SalesPlatform salesPlatform) {
        List<YgShoppingCar> list = ygShoppingCarMapper.selectBySkuIds(skuId == null ? null : new Long[]{skuId}, pType,
                userId, salesPlatform);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<YgShoppingCar> findBySkuIds(Long[] skuIds, PromotionType pType, Long userId) {
        return ygShoppingCarMapper.selectBySkuIds(skuIds, pType, userId, SalesPlatform.Yiigoo);
    }

    @Override
    public YgShoppingCar findBySkuIdWithSku(Long skuId, PromotionType pType, Long userId) {
        List<YgShoppingCar> list = ygShoppingCarMapper.selectBySkuIdsWithSku(skuId == null ? null : new Long[]{skuId}, pType, userId);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<YgShoppingCar> findBySkuIdsWithSku(Long[] skuIds, PromotionType pType, Long userId) {
        return ygShoppingCarMapper.selectBySkuIdsWithSku(skuIds, pType, userId);
    }

    @Override
    public List<YgShoppingCarModel> findIsCheck(Long userId) {
        return this.findIsCheck(userId, false);
    }

    @Override
    public List<YgShoppingCarModel> findIsCheck(Long userId, Boolean distribution) {
        return this.findIsCheck(userId, distribution, SalesPlatform.Yiigoo);
    }

    @Override
    public List<YgShoppingCarModel> findIsCheck(Long userId, Boolean distribution, SalesPlatform salesPlatform) {
        List<YgShoppingCarModel> list = ygShoppingCarMapper.selectIsCheck(userId,
                salesPlatform == null ? SalesPlatform.Yiigoo.name() : salesPlatform.name());
        boolean vipNew = isVipNew(userId, salesPlatform);
        list.forEach(carModel -> resetCarSalePrice(carModel, vipNew, distribution));
        return list;
    }

    @Override
    public int updateCarForCheck(Long[] skuIds, Long userId) {
        return updateCarForCheck(skuIds, userId, SalesPlatform.Yiigoo);
    }

    @Override
    public int updateCarForCheck(Long[] skuIds, Long userId, SalesPlatform salesPlatform) {
        // 现将购物车isCheck 更新为false
        YgShoppingCarExample example1 = new YgShoppingCarExample();
        example1.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andUserIdEqualTo(userId)
                .andSalesPlatformEqualTo(salesPlatform.name());
        YgShoppingCar record1 = new YgShoppingCar();
        record1.setIsCheck(false);
        ygShoppingCarMapper.updateByExampleSelective(record1, example1);
        // 将此次购买的商品设置为true
        YgShoppingCarExample example = new YgShoppingCarExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andUserIdEqualTo(userId)
                .andSkuIdIn(Arrays.asList(skuIds)).andSalesPlatformEqualTo(salesPlatform.name());
        YgShoppingCar record = new YgShoppingCar();
        record.setIsCheck(true);
        return ygShoppingCarMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int findItemCount(Long userId) {
        return findItemCount(userId, SalesPlatform.Yiigoo);
    }

    @Override
    public int findItemCount(Long userId, SalesPlatform salesPlatform) {
        return ygShoppingCarMapper.selectItemCount(userId, salesPlatform);
    }

    @Override
    public int deleteCarByCheck(Long userId, SalesPlatform salesPlatform) {
        return ygShoppingCarMapper.deleteCarByCheck(userId,salesPlatform);
    }

    @Override
    public YgShoppingCarModel findShoppingCarById(Long carId) {

        return ygShoppingCarMapper.selectShoppingCarByCarId(carId);
    }

    private Map<Long, List<YgShoppingCarModel>> getCarGroup(List<YgShoppingCarModel> list, Long userId) {
        Boolean isNew = ygUserService.isNewCustom(userId);
        for (YgShoppingCarModel car : list) {
            if (car.getSkuStatus().equals(DataStatus.Delete)) {
                car.setStockNum(0L);
            }
            if (!isNew) {
                car.setNewCustomPrice(new BigDecimal("0.00"));
            }
            if (car.getPackId() == null) {
                if (car.getSalesType().equals(PromotionType.Ordinary)) {
                    // 多买多折商品
                    List<BmdismModel> bmdisms = ygBmdismService.findByProductIds(car.getProductId());
                    if (!CollectionUtils.isEmpty(bmdisms)) {
                        // BmdismModel bmdismModel =
                        // bmdisms.stream().sorted(Comparator.comparingInt(BmdismModel::getBuyCount).reversed()).findFirst().get();
                        car.setPackId(-bmdisms.get(0).getId());
                    }
                } else {
                    car.setPackId(Long.parseLong(car.getSalesType().hashCode() + ""));
                }
            }
            // 普通商品
            if (car.getPackId() == null) {
                car.setPackId(0L);
            }
            car.setIsCheck(true);
        }
        // 按不同场景分组。并将不同场景的商品按价格从高到低排序
        return list.stream().collect(Collectors.groupingBy(YgShoppingCarModel::getPackId));

    }

    @Override
    public Map<Long, List<YgShoppingCarModel>> sortCarMapV2(List<YgShoppingCarModel> list, Long userId) {
        Map<Long, List<YgShoppingCarModel>> collect = this.getCarGroup(list, userId);
        for (Entry<Long, List<YgShoppingCarModel>> e : collect.entrySet()) {
            List<YgShoppingCarModel> value = e.getValue();
            value.sort(new CarComparator());
        }
        return collect;

    }

    @Override
    public List<List<YgShoppingCarModel>> sortCarListV2(List<YgShoppingCarModel> list, Long userId) {

        List<List<YgShoppingCarModel>> lists = new LinkedList<>();
        Map<Long, List<YgShoppingCarModel>> collect = this.getCarGroup(list, userId);
        for (Entry<Long, List<YgShoppingCarModel>> e : collect.entrySet()) {
            List<YgShoppingCarModel> value = e.getValue();
            value.sort(new CarComparator());
            lists.add(value);
        }

        return lists;
    }

    @Override
    public List<List<YgShoppingCarModel>> sortCarList(List<YgShoppingCarModel> list, Long userId) {
        List<List<YgShoppingCarModel>> lists = new LinkedList<>();
        Boolean isNew = ygUserService.isNewCustom(userId);
        if (list != null && list.size() > 0) {
            Map<Long, List<YgShoppingCarModel>> subDetailMap = new LinkedHashMap<>();
            List<Long> keys = new ArrayList<>();
            Long i = 99999L;
            for (YgShoppingCarModel car : list) {
                if (car.getSkuStatus().equals(DataStatus.Delete)) {
                    car.setStockNum(0L);
                }
                if (!isNew) {
                    car.setNewCustomPrice(new BigDecimal("0.00"));
                }
                car.setIsCheck(true);
                List<YgShoppingCarModel> subDetails;
                if (car.getPackId() != null) {
                    // 自选包商品
                    subDetails = subDetailMap.get(car.getPackId());
                    if (subDetails == null) {
                        subDetails = new ArrayList<>();
                        subDetailMap.put(car.getPackId(), subDetails);
                        keys.add(car.getPackId());
                    }
                } else {
                    // 普通商品（包含新客商品）
                    if (car.getNewCustomPrice().compareTo(new BigDecimal("0")) > 0) {

                        subDetails = subDetailMap.get(car.getProductId());
                        if (subDetails == null) {
                            subDetails = new ArrayList<>();
                            subDetailMap.put(car.getProductId(), subDetails);
                            keys.add(car.getProductId());
                        }
                    } else {
                        subDetails = subDetailMap.get(i);
                        if (subDetails == null) {
                            subDetails = new ArrayList<>();
                            subDetailMap.put(i, subDetails);
                            keys.add(i);
                        }
                        i++;
                    }
                }
                subDetails.add(car);

            }
            if (keys.size() > 0) {
                for (Long key : keys) {
                    List<YgShoppingCarModel> cars = subDetailMap.get(key);
                    cars.sort(new CarComparator());
                    lists.add(cars);
                }
            }
        }

        return lists;
    }

    /**
     * 同一花色下，多个尺码只用一件商品参与新客价
     *
     * @param carList 购物车
     */
    @Override
    public void doNewCusBussness(List<YgShoppingCarModel> carList) {
        Map<Long, List<YgShoppingCarModel>> collect = carList.stream().filter(car -> car.getNewCustomPrice().compareTo(new BigDecimal(0)) > 0).collect
                (Collectors.groupingBy(YgShoppingCarModel::getProductId));
        collect.forEach((k, v) -> {
            v.sort(new CarComparator());
            int[] idx = {0};
            v.forEach(car -> {
                if (idx[0] > 0) {
                    car.setNewCustomPrice(new BigDecimal(0));
                }
                idx[0]++;
            });
        });

    }

    @Override
    public Boolean hasOtherNewCus(Long skuId, Long userId) {
        YgSku sku = skuService.findById(skuId);
        if (sku.getNewCustomPrice().compareTo(new BigDecimal(0)) > 0) {
            return ygShoppingCarMapper.hasOtherNewCus(skuId, userId, sku.getProductId());
        }
        return false;
    }

    @Override
    public List<YgShoppingCarModel> findItemInfoPt(List<YgPtSkuInfo> skuList, Long userId) {
        List<YgShoppingCarModel> list = ygShoppingCarMapper.selectItemInfoPt(skuList, userId);
        Boolean vip = isVipNew(userId, SalesPlatform.Yiigoo);
        for (YgShoppingCarModel model : list) {
            for (YgPtSkuInfo sku : skuList) {
                if (sku.getSkuId().equals(model.getSkuId())) {
                    model.setQuantity(sku.getQuantity());
                }
                if (vip && model.getVipPrice().compareTo(new BigDecimal(0)) > 0) {
                    model.setRealSalesPrice(model.getVipPrice());
                    model.setPromotionPrice(model.getVipPrice());
                    model.setSalesPrice(model.getVipPrice());
                } else {
                    model.setRealSalesPrice(model.getPromotionPrice());
                    model.setSalesPrice(model.getPromotionPrice());
                }
                model.setCreateTime(new Date());
            }
        }
        return list;
    }

    private List<YgShoppingCarModel> getCarListForPt(List<YgPtSkuInfo> skuList, Long userId, Boolean distribution,
                                                     SalesPlatform salesPlatform) {
        List<YgShoppingCarModel> carModelList = ygShoppingCarMapper.selectItemInfoPt(skuList, userId);
        boolean vipNew = isVipNew(userId, salesPlatform);
        carModelList.forEach(carModel -> this.resetCarSalePrice(carModel, vipNew, distribution));
        return carModelList;
    }

    @Override
    public List<YgShoppingCarModel> findItemInfoPt(List<YgPtSkuInfo> skuList, Long userId, Boolean distribution) {
        return findItemInfoPt(skuList, userId, distribution, SalesPlatform.Yiigoo);
    }

    @Override
    public List<YgShoppingCarModel> findItemInfoPt(List<YgPtSkuInfo> skuList, Long userId, Boolean distribution,
                                                   SalesPlatform salesPlatform) {
        List<YgShoppingCarModel> list = this.getCarListForPt(skuList, userId, distribution, salesPlatform);
        for (YgShoppingCarModel model : list) {
            for (YgPtSkuInfo sku : skuList) {
                if (sku.getSkuId().equals(model.getSkuId())) {
                    model.setQuantity(sku.getQuantity());
                }
                model.setCreateTime(new Date());
            }
        }
        return list;
    }

    @Override
    public List<YgShoppingCarModel> findItemInfo(List<BaseSkuInfo> skuList, Long userId) {
        List<YgShoppingCarModel> list = ygShoppingCarMapper.selectItemInfo(skuList, userId);
        for (YgShoppingCarModel model : list) {
            for (BaseSkuInfo sku : skuList) {
                if (sku.getSkuId().equals(model.getSkuId())) {
                    model.setQuantity(sku.getQuantity());
                }
                model.setCreateTime(new Date());
            }
        }
        return list;
    }

    @Override
    public List<YgShoppingCarModel> findItemInfo(List<BaseSkuInfo> skuList, Long userId, PromotionType promotionType) {
        return this.findItemInfo(skuList, userId, promotionType, false);
    }

    @Override
    public List<YgShoppingCarModel> findItemInfo(List<BaseSkuInfo> skuList, Long userId, PromotionType promotionType,
                                                 Boolean distribution) {
        return findItemInfo(skuList, userId, promotionType, distribution, SalesPlatform.Yiigoo);
    }

    @Override
    public List<YgShoppingCarModel> findItemInfo(List<BaseSkuInfo> skuList, Long userId, PromotionType promotionType,
                                                 Boolean distribution, SalesPlatform salesPlatform) {
        if (CollectionUtils.isEmpty(skuList)) {
            return new ArrayList<>();
        }
        List<YgShoppingCarModel> list = ygShoppingCarMapper.selectItemInfoByPromotionType(skuList, userId,
                promotionType == null ? null : promotionType.name());
        boolean vipNew = isVipNew(userId, salesPlatform);
        Map<Long, List<BaseSkuInfo>> map = skuList.stream().collect(Collectors.groupingBy(BaseSkuInfo::getSkuId));
        for (YgShoppingCarModel car : list) {
            if (CollectionUtils.isEmpty(map.get(car.getSkuId()))) {
                continue;
            }
            BaseSkuInfo skuInfo = map.get(car.getSkuId()).get(0);
            car.setQuantity(skuInfo.getQuantity());
            car.setCreateTime(new Date());
            if (!PromotionType.Ordinary.equals(promotionType)) {
                continue;
            }
            resetCarSalePrice(car, vipNew, distribution);
        }
        return list;
    }

    @Override
    public List<YgShoppingCarModel> findShoppingCarListBySkuIds(Long[] skuIds, Long userId, Boolean distribution) {
        return findShoppingCarListBySkuIds(skuIds, userId, distribution, SalesPlatform.Yiigoo);
    }

    @Override
    public List<YgShoppingCarModel> findShoppingCarListBySkuIds(Long[] skuIds, Long userId, Boolean distribution,
                                                                SalesPlatform salesPlatform) {
        List<YgShoppingCarModel> carModelList = ygShoppingCarMapper.selectShoppingCarListBySkuIds(skuIds, userId,
                salesPlatform.name());
        boolean vipNew = isVipNew(userId, salesPlatform);
        carModelList.forEach(carModel -> this.resetCarSalePrice(carModel, vipNew, distribution));
        return carModelList;
    }

    @Override
    public List<ShoppingCarBasicData> findCarBasicDataList(Long userId, SalesPlatform salesPlatform) {
        return ygShoppingCarMapper.selectCarBasicDataList(userId, salesPlatform);
    }

    @Override
    public List<ShoppingCarBasicData> findCarBasicDataListBySkuId(Long userId, PromotionType salesType, List<Long> skuList) {
        return ygShoppingCarMapper.selectCarBasicDataListBySkuId(userId, salesType, skuList);
    }

    private boolean isVipNew(Long userId, SalesPlatform salesPlatform) {
        String platformNo = null;
        if (SalesPlatform.Yiigoo.equals(salesPlatform)) {
            platformNo = Constant.YIIGOO_PLATFORM_NO;
        } else if (SalesPlatform.Yijiadian.equals(salesPlatform)) {
            platformNo = Constant.YIJIADIAN_PLATFORM_NO;
        }
        return ygUserService.isVipNew(userId, platformNo);
    }
}
